#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUF_SIZE 30

int main(int argc,char *argv[])
{
    int sd;
    FILE *fp;
    char buf[BUF_SIZE];
    size_t read_cnt;
    struct sockaddr_in serv_addr;

    fp = fopen("receive.recv", "wb");
    sd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = PF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    serv_addr.sin_port = htons((uint16_t)atoi(argv[2]));

    connect(sd, (const struct sockaddr *)&serv_addr, sizeof(serv_addr));

    while(read_cnt = (size_t)read(sd, buf, BUF_SIZE)) {
        fwrite((void *)buf, 1, read_cnt, fp);
    }
    puts("接受到的数据");
    write(sd, "Thank you", 10);

    fclose(fp);
    close(sd);
    return 0;
}
